#version 330

#define PI 3.1415926535

float sdCircle(vec2 p, float r) {
    return length(p) - r;
}

void main() {    
    vec2 uv = ( gl_FragCoord.xy - 0.5 * iResolution.xy ) / iResolution.y;
    uv *= 2.0;          // [-0.5, 0.5] -> [-1, 1]
    float innerCircle = sdCircle(uv, 0.2);
    float outerCircle = sdCircle( uv, 0.4 );
    float inner_distance = smoothstep( 0.0, 0.01, innerCircle ); 
    float outer_distance = smoothstep( 0.0, 0.01, outerCircle );   
    /*  点乘方法
    // SDF 画了两个圆 整个画布宽度是[-1, 1]  两个半径是 0.2/0.4 
    // 当前处理的片元位置 传入 sdCircle 计算与两个圆 边界 的距离!!!! 注意是边界
    // 所以避免出错 且阅读逻辑正确 给 outer_distance-inner_distance 加上 abs
    float degree = 0.0;
    vec2 upVec = vec2( cos( radians(degree) ), sin( radians(degree) ) );
    vec2 nv1 = normalize( uv );
    float theta = acos( dot( nv1, upVec ) );
    theta /= PI;

    float belt = abs( outer_distance - inner_distance );
    belt = clamp( belt, 0.0, 1.0 );
    // 通过反三角函数求的角度值 范围是 [-PI, PI] 除以PI之后才归一化 
    // 并且此处直接使用的是角度值作为红色通道系数  所以接近0度的地方红色越淡
    gl_FragColor = vec4(belt * theta, 0.5, 0.5, 1.0);
    */

    /* 叉乘方法 最终方案 */
    float degree = 0.0;
    vec2 upVec = vec2( cos( radians(degree) ), sin( radians(degree) ) );
    vec2 nv1 = normalize( uv );

    float theta = acos( dot( nv1, upVec ) );
    vec3 n = cross( vec3( upVec, 0.0 ), vec3( nv1, 0.0 ) );
    theta *= sign( n.z );
    float belt = abs( outer_distance - inner_distance );

    // 映射
    float t = ( theta - (-PI) ) / ( PI - (-PI) );   // float t=（input-in_start)/（in_end-in_start); 计算theta在当前范围中的比例
    theta = 0.0 + ( 1.0 - 0.0 ) * t;                // out_start+（out_end-out_start)*t             将theta的比例用在[0, 1]的范围中
    gl_FragColor = vec4(1.0*theta*belt, 0.5, 0.5, 1.0);
}